﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FullViewerWindow.xaml
    /// </summary>
    public partial class FullViewerWindow : Window
    {
        public event EventHandler WantNextImage;
        public event EventHandler WantPreviousImage;
        public event EventHandler WantDeleteImage;

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register(
                "FileImageViewModel",
                typeof(FileImageViewModel),
                typeof(FullViewerWindow),
                new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FullViewerWindow source = d as FullViewerWindow;
            FileImageViewModel v = e.NewValue as FileImageViewModel;

            source.UpdateFileImageViewModel(v);
            source.InvalidateVisual();
        }

        private void UpdateFileImageViewModel(FileImageViewModel viewModel)
        {
            FileImageViewModel = viewModel;
            this.DataContext = viewModel;
            fullView.FileImageViewModel = viewModel;
        }

        public void FitOriginalSize()
        {
            fullView.Width = FileImageViewModel.OriginalWidth;
            fullView.Height = FileImageViewModel.OriginalHeight;
        }

        public System.Drawing.Rectangle WorkingArea
        {
            get { return Screen.PrimaryScreen.WorkingArea; }
        }

        public void FitToScreen()
        {
            System.Drawing.Rectangle area = WorkingArea;
            fullView.Width = area.Width;
            fullView.Height = area.Height - 6;
            fullView.imgMain.Width = area.Width;
            fullView.imgMain.Height = area.Height - 6;
        }

        public FullViewerWindow()
        {
            InitializeComponent();
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
            else if (e.Key == Key.OemPlus)
            {
                fullView.Zoom();
            }
            else if (e.Key == Key.Delete)
            {
                if (WantDeleteImage != null)
                    WantDeleteImage(this, EventArgs.Empty);
            }
            else if (e.Key == Key.OemMinus)
            {
                fullView.Shrink();
            }
            else if (e.Key == Key.F)
            {
                FitToScreen();
            }
            else if (e.Key == Key.O)
            {
                FitOriginalSize();
            }
            else if (e.Key == Key.NumPad3 || e.Key == Key.PageDown)
            {
                if (WantNextImage != null)
                    WantNextImage(this, EventArgs.Empty);
            }
            else if (e.Key == Key.NumPad9 || e.Key == Key.PageUp)
            {
                if (WantPreviousImage != null)
                    WantPreviousImage(this, EventArgs.Empty);
            }
            else if (e.Key == Key.Down)
            {
                fullView.scroller.ScrollToVerticalOffset(fullView.scroller.VerticalOffset + 100);
            }
            else if (e.Key == Key.Up)
            {
                fullView.scroller.ScrollToVerticalOffset(fullView.scroller.VerticalOffset - 100);
            }
            else if (e.Key == Key.Right)
            {
                fullView.scroller.ScrollToHorizontalOffset(fullView.scroller.HorizontalOffset + 100);
            }
            else if (e.Key == Key.Left)
            {
                fullView.scroller.ScrollToHorizontalOffset(fullView.scroller.HorizontalOffset - 100);
            }
            else
            {
                string s = e.Key.ToString();
                if (s[0] == 'D')
                {
                    char c = s[1];
                    
                    if (c >= '0' && c <= '9')
                    {
                        Window1._metaData.AddMetaData(FileImageViewModel.FileName, (double)(c - '0'));
                    }
                }
            }
        }
    }
}
